package com.itany.cms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.itany.cms.constant.ResponseCodeConstant;
import com.itany.cms.entity.CourseType;
import com.itany.cms.exception.COurseTypeStatusErrorException;
import com.itany.cms.exception.CourseTypeExistException;
import com.itany.cms.exception.RequestParameterErrorException;
import com.itany.cms.service.CourseTypeService;
import com.itany.cms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/type")
public class CourseTypeController {
    @Autowired
    private CourseTypeService typeService;

    @RequestMapping("/findType")
    public ModelAndView findType(CourseType courseTypeParam,  String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();

        PageInfo<CourseType> courseTypePage = typeService.findByParams(courseTypeParam, pageNo, pageSize);
        mav.addObject("typePage", courseTypePage);
        mav.setViewName("backend/back_courseTypeSet");
        return mav;
    }


    @RequestMapping("/findFatherType")
    public ModelAndView findFatherType(@RequestParam("id") String id, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        CourseType courseTypeParam =new CourseType();
        courseTypeParam.setParentId(Integer.parseInt(id));
        PageInfo<CourseType> courseTypePage = typeService.findFatherType(courseTypeParam, pageNo, pageSize);
        mav.addObject("typePage", courseTypePage);
        mav.setViewName("backend/back_courseTypeSet");
        return mav;
    }
    @RequestMapping("/findSonType")
    public ModelAndView findSonType(@RequestParam("id") String id, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        CourseType courseTypeParam = new CourseType();
        courseTypeParam.setId(Integer.parseInt(id));
        PageInfo<CourseType> courseTypePage = typeService.findSonType(courseTypeParam, pageNo, pageSize);
        mav.addObject("typePage", courseTypePage);
        mav.setViewName("backend/back_courseTypeSet");
        return mav;
    }

    @ResponseBody
    @RequestMapping("/addFatherType")
    public ResponseResult addFatherType(String typeName) {
        ResponseResult result = new ResponseResult();
        try {
            typeService.addFatherType(typeName);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (CourseTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/addSonType")
    public ResponseResult addSonType(String fatherId, String typeName) {
        ResponseResult result = new ResponseResult();
        try {
            typeService.addSonType(fatherId, typeName);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (CourseTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/modifyType")
    public ResponseResult modifyType(String id, String name) {
        ResponseResult result = new ResponseResult();

        try {
            typeService.modifyName(id, name);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (CourseTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }


        return result;
    }

    @ResponseBody
    @RequestMapping("/enabled")
    public ResponseResult enabledType(String id) {
        ResponseResult result = new ResponseResult();

        try {
            typeService.enabledType(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (COurseTypeStatusErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @ResponseBody
    @RequestMapping("/disabled")
    public ResponseResult disabledType(String id) {
        ResponseResult result = new ResponseResult();

        try {
            typeService.disabledType(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

}
